package controllers;

import play.*;
import play.data.validation.Valid;
import play.db.jpa.JPABase;
import play.mvc.*;

import java.util.*;

import jj.play.ns.com.jhlabs.image.RotateFilter;

import org.apache.commons.lang.StringUtils;

import notifiers.Mails;

import models.*;

public class Application extends Controller {

	@Before
	public static void setProductTypes(){
		ProductType rootProductType =	ProductType.findById(ProductType.getRootId());
		StringBuffer productTree = new StringBuffer();
		buildProductTree(productTree,rootProductType.children,true);
		renderArgs.put("productTree", productTree.toString());
	}
	
    public static void index() {
        render();
    }
    
    
    public static void homepage(){
    	List<Product> products = Product.findHomePageProducts(4);
    	render(products);
    }
    
    public static void listProducts(Long productTypeId){    	
    	Map<String, Object> args = new HashMap<String, Object>();
    	args.put("productTypeId", productTypeId);
    	redirect(Router.reverse("Front.productList",args).toString(), true);
    }
    
    public static void listProductTypes(){
    	listProducts(null);
    
    }
    
    public static void overview(Long productId){
    	Map<String, Object> args = new HashMap<String, Object>();
    	args.put("productId", productId);
    	redirect(Router.reverse("Front.productDetails",args).toString(), true);
    	
    }
    
    private static void buildProductTree(StringBuffer productTree,List<ProductType> productTypes,boolean first){		
        if(productTypes!=null && productTypes.size()>0){
        	if(first){
        		productTree.append("<ul class='sub'>");
        	}else{
        		productTree.append("<ul>");
        	}
        	for (ProductType productType : productTypes) {
        		Map<String, Object> args = new HashMap<String, Object>();
				args.put("productTypeId", productType.id);
				if(productType.children.size()>0){
					productTree.append("<li><a class='fly' href='");
				}else{
					productTree.append("<li><a href='");
				}
				productTree.append(Router.reverse("Application.listProducts", args));
				productTree.append("'>");
				productTree.append(productType.name);
				productTree.append("</a>\n");
				buildProductTree(productTree,productType.children,false);
				productTree.append("</li>\n");
			}
        	productTree.append("</ul>\n");
        }
	}
    
    public static void about(){
    	redirectTo("about",null);
    }
    
    
    public static void comments(){
    	render();
    }
    
    public static void hygiene(){
    	redirectTo("hygiene","teeth-whitening-products");
    }
    
    public static void distributors(){
    	render();
    }
    
    public static void  delivery(){
    	render();
    	
    }
    
    public static void payment(){
    	render();
    }
    
    public static void contact(){
    	redirectTo("contact",null);
    }
    
    private static void redirectTo(String type1,String type2){
    	Map<String, Object> args = new HashMap<String, Object>();
    	args.put("type1", type1);
    	args.put("type2", type2);
    	redirect(Router.reverse("Front.viewContent",args).toString(), true);
    }
    
    public static void submitAppointment(@Valid Appointment a){
    	if(validation.hasErrors()) {
	       params.flash(); // add http parameters to the flash scope
	       validation.keep(); // keep the errors for the next request
	       about();
	   }else{
	    	String[] interest = params.getAll("a.productInterest");
	    	String interests = StringUtils.join(interest,",");
	    	Logger.debug("interests: %s,%s",interest.length,interests);
	    	a.productInterest = interests;
	    	
	    	a.save();
	    	Mails.sendAppointment(a);
	    	flash.success("Thank you! Your appointment have been submited successful!");
	    	flash("msg", "");
	    	about();
	   }
    }
    
    public static void submitComments(@Valid Comment c){
    	if(validation.hasErrors()){
    		params.flash();
    		validation.keep();
    		comments();
    	}else{
    		c.save();
    		Mails.sendComment(c);
    		flash.success("Thank you! Your comment have been submited successful!");
    		comments();
    	}
    }

    public static void underConstruction(){
    	render();
    }
}